#import irc
import imp
from commands import *

class CommandHandler():
    def __init__(self, irc):
        self.irc = irc
        self.events = {}
        self.events['NICK'] = Nick(self.nick)
        self.events['PRIVMSG'] = Privmsg(self.privmsg)
        self.events['KICK'] = Kick(self.kick)
        self.events['JOIN'] = Join(self.join)
        self.events['PART'] = Part(self.part)
        num_handler = Numeric(self.numeric)
        self.events['332'] = num_handler
        self.events['353'] = num_handler
        self.events['366'] = num_handler
        self.events['QUIT'] = Quit(self.quit)
        self.events['MODE'] = Mode(self.mode)
        self.events['324'] = num_handler
        self.events['TOPIC'] = Topic(self.topic)
        self.events['333'] = num_handler
    def dispatch(self, sender, args):
        event = args[1]

        #change to event in self.events for py3
        if event in self.events.keys():
            self.events[event].emit(sender, args)

    def privmsg(self, sender, args):
 
        return None
    def nick(self, sender, args):
        for ch in self.irc.chmgr.channels:
            self.irc.chmgr.get(ch).del_names(sender.nick)
            self.irc.chmgr.get(ch).add_name(args[0])
    def kick(self, sender, args):
        if sender.nick == self.irc.nickname:
            self.irc.chmgr.parting(args[2])
        else:
            self.irc.chmgr.get(args[2]).del_names(args[3])
    def mode(self, sender, args):
        rdata = self.irc.modes(" ".join(args[3:]))
        for mode in rdata:
            self.irc.chmgr.get(args[2]).change_mode(mode[1], mode[2], mode[0])
    def join(self, sender, args):
        if sender.nick == self.irc.nickname:
            channel = self.irc.chmgr.joining(args[0])
            channel.start_names()
        else:
            self.irc.chmgr.get(args[0]).add_name(sender.nick)
    def quit(self, sender, args):
        for ch in self.irc.chmgr.channels:
            self.irc.chmgr.get(ch).del_names(sender.nick)                
    def part(self, sender, args):
        if sender.nick == self.irc.nickname:
            self.irc.chmgr.parting(args[0])
        else:
            self.irc.chmgr.get(args[2]).del_names(sender.nick)
    def numeric(self, sender, args):
        event = args[1]
        if event == '353':  #RPL_NAMREPLY
            self.irc.chmgr.get(args[4]).get_names(sender, args[0])
        elif event == '366':    #RPL_ENDOFNAMES
            self.irc.chmgr.get(args[3]).end_names()
            self.irc.send("MODE %s" % (args[3]))
        elif event == '332':    #RPL_TOPIC
            self.irc.chmgr.get(args[3]).topic = args[0]
        elif event == '333':
            self.irc.chmgr.get(args[3]).topic_time = args[5]
            self.irc.chmgr.get(args[3]).topic_setter = args[4]
        elif event == '324':
            for i in args[4][1:]:
                self.irc.chmgr.get(args[3]).change_mode(i,None,"+")
    def topic(self, sender, args):
        #server does not send a 333 message on a standard topic change, this forces the message
        self.irc.send("TOPIC %s" % args[2])
